package cn.autowired.exam.controller;

import cn.autowired.exam.base.CommonResponse;
import com.google.common.collect.Maps;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import cn.autowired.exam.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import cn.autowired.exam.service.QuestionService;

import cn.autowired.exam.entity.Question;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;

/**
 *
 *   @description : Question 控制器
 *   ---------------------------------
 */
@RestController
@RequestMapping("/question")
public class QuestionController extends AbstractController {
private final Logger logger=LoggerFactory.getLogger(QuestionController.class);

    @Autowired
    public QuestionService questionService;

    /**
     * 创建问题并填入试卷
     * @param title 问题标题
     * @param optionA A
     * @param optionB B
     * @param optionC C
     * @param optionD D
     * @param paperId 试卷id
     * @return
     */
    @RequestMapping("/add")
    public ResponseEntity addNewQuestion(String title,
                                         String optionA,
                                         String optionB,
                                         String optionC,
                                         String optionD,
                                         Integer paperId){
        Question question = new Question();
        question.setTitle(title);
        question.setOptionA(optionA);
        question.setOptionB(optionB);
        question.setOptionC(optionC);
        question.setOptionD(optionD);
        question.setPaperId(paperId);
        HashMap<Object, Object> hashMap = Maps.newHashMap();
        boolean flag = question.insertOrUpdate();

        return ResponseEntity.ok(flag?CommonResponse.ok():CommonResponse.error());

    }

    /**
     * 编辑问题并填入试卷
     * @param title 问题标题
     * @param optionA A
     * @param optionB B
     * @param optionC C
     * @param optionD D
     * @return
     */

    @RequestMapping("/edit")
    public ResponseEntity editQuestion(String title,
                                         String optionA,
                                         String optionB,
                                         String optionC,
                                         String optionD,
                                         Integer id){
        Question question = questionService.selectById(id);
        question.setTitle(title);
        question.setOptionA(optionA);
        question.setOptionB(optionB);
        question.setOptionC(optionC);
        question.setOptionD(optionD);
        HashMap<Object, Object> hashMap = Maps.newHashMap();
        boolean flag = question.insertOrUpdate();
        if (flag){
            hashMap.put("msg","success");
            hashMap.put("code",0);
            return ResponseEntity.ok(hashMap);
        } else {
            hashMap.put("msg","error");
            hashMap.put("code",-1);
            return ResponseEntity.ok(hashMap);
        }
    }

    /**
     * 删除问题并填入试卷
     *
     *
     *
     *
     *
     * @return
     */

    @RequestMapping("/delete")
    public ResponseEntity editQuestion(Integer id){
        Question question = questionService.selectById(id);
        boolean flag = question.deleteById();
        HashMap<Object, Object> hashMap = Maps.newHashMap();

        if (flag){
            hashMap.put("msg","success");
            hashMap.put("code",0);
            return ResponseEntity.ok(hashMap);
        } else {
            hashMap.put("msg","error");
            hashMap.put("code",-1);
            return ResponseEntity.ok(hashMap);
        }
    }

}